package tgc.edu.zz.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.zz.service.BlogImagService;
import tgc.edu.zz.service.BlogTypeService;
import tgc.edu.zz.vo.BlogTypeVo;


//个人博客
@Controller
@RequestMapping("/blog")
public class BlogController {
	
	
	@Autowired
	private BlogImagService imageService;
	@Autowired
	private BlogTypeService typeService;
	
	/**
	 * 
	 * @return 写博客页面
	 */
	@RequestMapping("/blog")
	public String upload() {
		return "blog/blog";
	}

	@RequestMapping("/blog2")
	public String blog2(HttpServletRequest request,ModelMap map) {
		List<BlogTypeVo> type = typeService.findAll();
		map.put("types", type);
		return "blog/blog2";
	}
	
	/**
	 * 上传博客图片
	 * @param file
	 * @return
	 */
	@RequestMapping("/upload")
	@ResponseBody
	public Object upload(MultipartFile file) {
		String uuid = UUID.randomUUID().toString();
		imageService.saveImage(file,uuid);
		return  "http://localhost/blog/download?uuid="+uuid;
	}
	
	/**
	 * 下载图片展示
	 * @param uuid
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(String uuid, HttpServletRequest request, HttpServletResponse response) {
		imageService.download(uuid, request, response);
		return null;
	}
	
	/**
	 * 显示我的博客页面（正在建设）
	 * @return
	 */
	@RequestMapping("/selectBlog")
	public String selectBlog() {
		return "error/404";
	}
}
